<script>
import base from '../form/modal';
import $ from '@/common';
export default {
	extends: base,
	created() {},
	methods: {
		load() {
			this.updateData();
		},
		updateData() {
			var findPath = this.getFindPath();
			if (findPath && !this.isLocalData) {
				let param = { ...this.$parent.actionData };
				this.dataLoading = true;
				$.link(findPath, null, {
					param: {
						...this.pathParam,
						...param,
					},
				})
					.then(res => {
						var data = res.data.model ? res.data.data : res.data;
						if (typeof this.$parent.actionData === 'string' && data.id === undefined) {
							data.id = this.$parent.actionData;
						} else if (this.$parent.actionData && this.$parent.actionData.id && data.id === undefined) {
							data.id = this.$parent.actionData.id;
						}
						this.setFormData(data);
					})
					.finally(() => {
						this.dataLoading = false;
					})
					.catch(ex => {
						this.close();
					});
			} else {
				let data = this.$parent.actionData;
				if (this.actionModel.locate !== 'inline') {
					data = this.$owner.getSelectedDataRow();
				}
				this.$set(this, 'data', data);
				this.dataLoading = false;
			}
		},
		setFormData(data) {
			this.formData = data;
			this.modelLoad = true;
			this.$nextTick(() => {
				this.$refs.form.setData(data);
				this.$refs.form.eventUpdate();
			});
		},
		actionSubmit() {
			if (this.isLocalData) {
				this.$owner.editRow({
					data: this.$refs.form.getData ? this.$refs.form.getData() : this.$refs.form.formData,
					index: this.$parent.actionDataIndex,
				});
				this.close();
			} else {
				return this.putData();
			}
		},
	},
};
</script>
